# marshmallow 映射文件
from marshmallow import fields
from marshmallow_sqlalchemy import ModelSchema

from application.users.models import *


class ClassSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Class
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    name = fields.String(required=True, error_messages={
        'required': 'classname required'
    })


class UsersSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Users
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    username = fields.String(required=True, error_messages={
        'required': 'username required'
    })
    fullname = fields.String(required=True, error_messages={
        'required': 'fullname required'
    })
    password = fields.String(required=True, load_only=True)
    status = fields.Integer()
    create_time = fields.DateTime(dump_only=True)
    current_listen_status = fields.String()
    avatar = fields.String(dump_only=True)
    # 嵌套json
    uclass = fields.Nested(ClassSchema)


class RoleSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Role
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    name = fields.String(required=True, error_messages={
        'required': 'username required'
    })
    code = fields.String(required=True, error_messages={
        'required': 'fullname required'
    })


class PermissionSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = Permission
        sqla_session = db.session

    id = fields.Integer(dump_only=True)
    name = fields.String(required=True, error_messages={
        'required': 'username required'
    })
    code = fields.String(required=True, error_messages={
        'required': 'fullname required'
    })


class RolePermissionSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = RolePermission
        sqla_session = db.session

    role_id = fields.Nested(RoleSchema)
    permission = fields.Nested(PermissionSchema)


class UsersRoleSchema(ModelSchema):
    class Meta(ModelSchema.Meta):
        model = UsersRole
        sqla_session = db.session

    user_id = fields.Nested(UsersSchema)
    role_id = fields.Nested(RoleSchema)
